package com.xjl.springbootnetty.netty.socketdemo;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName ServerSocket
 * @Author user
 * @Date 2021/11/09:15:42
 * @Description
 * @Version 1.0.0
 */
@Slf4j
public class ServerSocketImpl {

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(3142);
            Socket accept = serverSocket.accept();
            new Thread(() ->{
                doStart(accept);
            }).start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void doStart(Socket accept) {
        try {
            InputStream inputStream = accept.getInputStream();
            byte[] data = new byte[1024];
            int len;
            while ((len=inputStream.read(data)) != -1){
                String s = new String(data, 0, len);
                log.info("获取客户端的值:{}",s);
            }
            accept.getOutputStream().write("i‘m server".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
